<?php
$deId = $_GET['pid'] > 0 ? $_GET['pid'] : $_POST['deId'];

if ($deId < 1 || $deId == '') {

    header('Location: categorie_list.php');
    exit;
}

error_reporting(E_ALL);
include_once('../controller/includes/db_access.php');

$toonerror = array(
    1 => 'Controleren of alle velden zijn ingevuld!',
    2 => 'De opgegeven URL is niet geldig (voorbeeld: www.blivio.com)',
    3 => 'Het foto mag maximaal 2MB zijn',
    4 => 'Het foto moet minimaal 500 pixels breed zijn!',
    5 => 'Er is iets fout gegaan bij het uploaden van het foto!',
    6 => 'Deze naam bestaat al! Kies een andere naam'
);



//begin script voor bewerk eindproducten 


$deId = $_GET['pid'];
if (isset($_POST['actType']) && ($_POST['actType'] == 'opslaan')) {



    $from = $_POST['geldig_van'];
    $to = $_POST['geldig_tot'];

    $from = date('d-m-Y', strtotime($from));
    $to = date('d-m-Y', strtotime($to));

    $nodays = (strtotime($to) - strtotime($from)) / (60 * 60 * 24); //it will count no. of days






    if (!empty($_POST['name'])
            && !empty($_POST['type'])
            && !empty($_POST['code'])
            && !empty($_POST['geldig_van'])
            && !empty($_POST['geldig_tot'])
    ) {


//kijk of aantal dagen is groot dan 0
        if ($nodays > 0) {


            $naam = $_POST['name'];
;
           $code = $_POST['code'];
            $geldig_van = $_POST['geldig_van'];
            $geldig_tot = $_POST['geldig_tot'];
            $aktief = $_POST['aktief'];

            if ($_POST['type'] == 'C') {
                $type = $_POST['type'];
                if ($_POST['waarde'] > 0) {

                    $korting = $_POST['waarde'];
                    
                    $query_product = mysql_query("
                    UPDATE cadeaubon SET naam='$naam',
                    cadeaubon.type='$type',
                    korting=$korting,
                    cadeaubon.code='$code',
                    geldig_van='$geldig_van',
                    geldig_tot='$geldig_tot',
                    aktief='$aktief' WHERE cadeaubon_id=" . $_POST['deId']);
                    
            header('Location: korting_list.php');

                } else {
                     //$deId = $_GET['pid'];
                    $Error = "Controleren of de velden waarde is ingevuld!";

                }
            } elseif ($_POST['type'] == 'A') {
                 $type = $_POST['type'];
                if ($_POST['korting'] > 0) {
                    
                    $korting = $_POST['korting'];
                    
                $query_product = mysql_query("
                UPDATE cadeaubon SET naam='$naam',
                cadeaubon.type='$type',
                korting=$korting,
                cadeaubon.code='$code',
                geldig_van='$geldig_van',
                geldig_tot='$geldig_tot',
                aktief='$aktief' WHERE cadeaubon_id=" . $_POST['deId']);
                
           header('Location: korting_list.php');
                    
                    
                    
                } else {
                    //$deId = $_GET['pid'];
                    $Error = "Controleren of de veld Percentage is ingevuld!";
                   // exit ();
                }
            }

// $aValues['geldig_tot'] = STR_TO_DATE($_POST['geldig_tot'], '%d/%m/%Y');



        } else {
            header('Location: korting_edit.php?pid=' . $_GET['pid'] . '&error=2');
            exit;
        }
    } else {
        header('Location: korting_edit.php?pid=' . $_GET['pid'] . '&error=3');
        exit;
    }
}

// MAIN //
$query = "SELECT * FROM cadeaubon WHERE cadeaubon_id =" . $deId;
$result = mysql_query($query);
$num = mysql_numrows($result);
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <!--[if lt IE 9]>
        <script
        src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <link rel="stylesheet" href="styles/style.css" type="text/css" />
        <!--[if lte IE 7]>
        <link rel="stylesheet" href="style_ie.css" />
        <![endif]-->
        <title>dbme!!!!</title>
        <link rel="stylesheet" href="../css/themes/base/jquery.ui.all.css" />
        <script type="text/javascript" src="../js/lib/jquery.js"></script>
        <script type="text/javascript" src="../js/jquery-1.7.2.js"></script>
        <script type="text/javascript" src="../js/lib/chili-1.7.pack.js"></script>
        <script type="text/javascript" src="../js/lib/jquery.easing.js"></script>
        <script type="text/javascript" src="../js/lib/jquery.dimensions.js"></script>
        <script type="text/javascript" src="../js/jquery.ui.core.js"></script>
        <script type="text/javascript" src="../js/jquery.ui.widget.js"></script>
        <script type="text/javascript" src="../js/jquery.ui.datepicker.js"></script>

        <script type="text/javascript">

            ////jquery datepicker met aangepast datumformaat
  
            $(function() {
                $('input#datepicker,#datepicker1').datepicker({
                    dateFormat: 'dd-mm-yy',
                    changeMonth: false,
                    changeYear: false
                },
                $.datepicker.regional['nl']);
            });
        </script>

</head>
<body>
    <div id="bloc_page">
        <header>
            <a href="#"><img src="../images/dBmelogo.png" alt="referentie" alt="dbme!" /></a>
        </header>  
        <nav>
<?php include_once('header.php'); ?>
        </nav>
        <section>
            <aside>
                <!--right side baar -->  
            </aside>

            <article>               


                <table width="800" border="0" cellspacing="5" cellpadding="5">
                    <form action="korting_edit.php?pid=<?php echo $_GET['pid']; ?>" method="post" name="sform" id="sform" enctype="multipart/form-data" >
                        <input type="hidden" name="actType" value="opslaan" />
                        <input type="hidden" name="deId" value="<?php echo $_GET['pid']; ?>" />

                        <tr>
                            <td width="100" valign="top"> &nbsp;&nbsp;&nbsp;</td>
                            <td width="500" valign="top" colspan="2"><?php
if (isset($Error)) {
    echo '<font color="red">'.$Error.'</font>';
}
?></td>

                            <td width="200" valign="top"> &nbsp;&nbsp;&nbsp;</td>

                        </tr>


                        <tr>


                            <td  valign="top">Naam:</td>
                            <td valign="top">

                                <input type="text" name="name" value="<?php echo mysql_result($result, 0, "naam"); ?>" class="inp" onblur="this.style.backgroundColor='#fff';"  style="width:200px" /><font color="red" >*</font>

                            </td>
                            <td valign="top" colspan="2">
                            </td>
                        </tr>

                        <tr>

                            <td colspan="4" height="30" valign="top">&nbsp;&nbsp;&nbsp;</td>

                        </tr>

                        <tr>
                            <td  valign="top">Dit is een:</td>
                            <td valign="top">
                                <input name="type" type="radio" value="C" <?php if (mysql_result($result, 0, "type") == 'C')
    echo ' checked'; ?> />Kortingsbedrag (een malig gebruik!) <br />
                                <input name="type" type="radio" value="A" <?php if (mysql_result($result, 0, "type") != 'C')
    echo ' checked'; ?> />Kortingspercentage
                            </td>
                            <td>
                                waarde &nbsp; &nbsp; &nbsp; &nbsp;<input type="text" name="waarde" value="<?php if (mysql_result($result, 0, "type") == 'C')
    echo mysql_result($result, 0, "korting"); ?>" class="inp" onblur="this.style.backgroundColor='#fff';"  style="width:60px" /> &euro;<br />
                                Percentage &nbsp;<input type="text" name="korting" value="<?php if (mysql_result($result, 0, "type") == 'A')
                                           echo mysql_result($result, 0, "korting"); ?>" class="inp" onblur="this.style.backgroundColor='#fff';"  style="width:60px" /> %
                            </td>
                            <td  valign="top"></td>
                        </tr>
                        <tr>

                            <td colspan="4" height="30" valign="top"> &nbsp;&nbsp;&nbsp;</td>

                        </tr>

                        <tr>
                            <td valign="top"> code: </td>
                            <td valign="top">


                                <input type="text" name="code" value="<?php echo mysql_result($result, 0, "code"); ?>" class="inp" onblur="this.style.backgroundColor='#fff';"  style="width:150px" />
                            </td>
                            <td valign="top"> &nbsp;&nbsp;&nbsp;</td>
                            <td valign="top"> &nbsp;&nbsp;&nbsp;</td>

                        </tr>

                        <tr>
                            <td valign="top"> Geldig van: </td>
                            <td valign="top">  
                                <input type="text" name="geldig_van" value="<?php echo mysql_result($result, 0, "geldig_van"); ?>"  id="datepicker" onblur="this.style.backgroundColor='#fff';"  style="width:150px" /><font color="red" >*</font>
                            </td>
                            <td valign="top"> &nbsp;&nbsp;&nbsp;</td>
                            <td valign="top"> &nbsp;&nbsp;&nbsp;</td>

                        </tr>
                        <tr>
                            <td valign="top"> Geldig tot: </td>
                            <td valign="top">

                                <input type="text" name="geldig_tot" value="<?php echo mysql_result($result, 0, "geldig_tot"); ?>" id="datepicker1" onblur="this.style.backgroundColor='#fff';"  style="width:150px" /><font color="red" >*</font>
                            </td>
                            <td valign="top"> &nbsp;&nbsp;&nbsp;</td>
                            <td valign="top"> &nbsp;&nbsp;&nbsp;</td>

                        </tr>
                        <tr>

                            <td colspan="4" height="30" valign="top"> &nbsp;&nbsp;&nbsp;</td>

                        </tr>
                        <tr>
                            <td valign="top"> Aktief</td>
                            <td valign="top">
                                <input name="aktief" type="radio" value="Y" <?php if (mysql_result($result, 0, "aktief") == 'Y')
                                           echo ' checked'; ?> /> Ja &nbsp;&nbsp;&nbsp;&nbsp;
                                <input name="aktief" type="radio" value="N" <?php if (mysql_result($result, 0, "aktief") != 'Y')
                                           echo ' checked'; ?> /> Nee
                            </td>
                            <td valign="top"> &nbsp;&nbsp;&nbsp;</td>
                            <td valign="top"> &nbsp;&nbsp;&nbsp;</td>
                        </tr>

                        <tr>
                            <td>&nbsp;</td>
                            <td align="left">&nbsp;</td>
                            <td align="left"><input type="submit" name="opslaan" id='cmdSubmit' value="save" /></td>

                            <td align="left">&nbsp;</td>
                        </tr>
                    </form>
                </table>
                </p>

            </article>

            <aside>
                <!--right side baar -->


            </aside>
        </section>

        <footer>  
            <p>Lorem ipsum dolor sit amet...</p>
            <p>Vivamus sed libero nec mauris pulvinar facilisis ut non sem...</p>
            <p>Phasellus ligula massa, congue ac vulputate non, dignissim at augue...</p>
        </footer>
    </div>
</body>
</html>